package com.gg.fruit.controlller;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.gg.entity.Fruit;
import com.gg.entity.User;
import com.gg.fruit.service.FruitService;
import com.gg.star.service.StarService;

import helpers.baseservlet.BaseServlet;
import povos.StarFruitPovo;

@WebServlet("/fruit/SearchFruitByFidServlet")
public class SearchFruitByFidServlet extends BaseServlet {
	@Autowired
	private FruitService fruitService ;
	@Autowired
	private StarService starService;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		User user=(User) request.getSession().getAttribute("user");
		if(user==null) {
			response.sendRedirect("/fruits/login.jsp");
			return;
			
			
		}
		String uid =user.getUid();
		String fid=request.getParameter("fid");
		StarFruitPovo povo=starService.getStarFruitPovoByUidAndFid(fid,uid);
		if (povo==null) {
			request.getSession().removeAttribute("starflag");
			
		}else {
			request.getSession().setAttribute("starflag","134");
			
			
		}
		Fruit fruit=fruitService.getFruitByid(fid);
		
		String path =this.getServletContext().getRealPath("/img/fruits/"+fid);
		File file =new File (path);
		int imgcount=file.listFiles().length;
		
		
		request.getSession().setAttribute("imgcount", imgcount);
		request.getSession().setAttribute("thisfruit", fruit);
		response.sendRedirect("/fruits/fruit_info.jsp");
	}

	

}
